package CI.aufgabe2.cip.mynodes;

import CI.aufgabe2.cip.SymbolTable;
import CI.aufgabe2.cip.base.AbstractDescr;
import CI.aufgabe2.cip.descriptor.SimpleTypeDescr;

public class MyIdNode extends MyAbstractNode {

	private static final long serialVersionUID = 1L;

	String strval;
	boolean var;
	
	public MyIdNode() {
		this.strval = "";
		this.var=false;
			};

	public MyIdNode(String fs) {
		strval = fs;
	};

	public void setVar(boolean bool){
		this.var=bool;
	}
	public boolean getVar(){
		return this.var;
	}
	public void print() {
		trace("MyIdNode " + strval);
		unindent();
	};
	
	public AbstractDescr compile(SymbolTable sym) {
		int vLevel = sym.currentLvl() - sym.getLevelOf(strval);
		if(vLevel == 0){
				write("PUSHI, " + sym.getAdr(strval));
		}else if(vLevel == sym.currentLvl()){
			write("PUSHI, " + sym.getAdr(strval));
			write("GETFP");
			write("ADD");
		}else{
			write("PUSHI, " + vLevel);
			write("GETSL");
			write("PUSHI, " + sym.getAdr(strval));
			write("ADD");
		}
		AbstractDescr descr =sym.getDescr(strval);
		return descr;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((strval == null) ? 0 : strval.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MyIdNode other = (MyIdNode) obj;
		if (strval == null) {
			if (other.strval != null)
				return false;
		} else if (!strval.equals(other.strval))
			return false;
		return true;
	}

	public String toString() {
		return strval;
	}
}
